package com.shenhaofeng.demo;

import android.app.Application;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shenhaofeng.app.component.ActivityLifecycleManager;
import com.shenhaofeng.app.download.Downloader;
import com.shenhaofeng.app.http.CacheControl;
import com.shenhaofeng.app.http.Call;
import com.shenhaofeng.app.http.HttpClient;
import com.shenhaofeng.app.http.Params;
import com.shenhaofeng.app.http.ResponseCallback;
import com.shenhaofeng.app.http.Result;
import com.shenhaofeng.app.http.ResultCallback;
import com.shenhaofeng.app.http.adapter.GsonAdapter;
import com.shenhaofeng.app.http.okhttp.OkHttpCreator;
import com.shenhaofeng.app.http.request.Entry;
import com.shenhaofeng.app.http.request.Request;
import com.shenhaofeng.app.http.response.Response;
import com.shenhaofeng.app.http.type.Types;
import com.shenhaofeng.app.http.url.HttpParamsUrl;
import com.shenhaofeng.app.log.CaptureErrorFileHandler;
import com.shenhaofeng.app.log.GlobalErrorInitializer;

import java.io.IOException;
import java.lang.reflect.TypeVariable;
import java.net.UnknownServiceException;
import java.util.ArrayList;

import rx.Observable;
import rx.functions.Action1;


/**
 * Created by Administrator on 2016/9/14.
 */
public class AppContext extends Application {

    private CaptureErrorFileHandler captureErrorFileHandler;

    HttpClient httpClient = new HttpClient();


    @Override
    public void onCreate() {
        super.onCreate();
        Downloader.init(this);
        //注册Activity生命周期管理
        ActivityLifecycleManager.init(this, true);
        //注册全局错误处理
        GlobalErrorInitializer.init(captureErrorFileHandler = new CaptureErrorFileHandler(this));
        httpClient.creator(new OkHttpCreator()
                .adapter(new GsonAdapter(new Gson())));
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    Result<com.shenhaofeng.demo.Result<User>> result =
                    Call call = httpClient.newCall(new Request.Builder()
                            .url(HttpParamsUrl.create("http://lsapp.qingniuhui.com/User/GetUserinfo1", new Params.Builder()
                                    .put("test", "testvalue")
                                    .build()))
                            .post(new Entry("key", "PEx7xDTBT"))
                            .cacheControl(new CacheControl.Builder()
                                    .noCache()
                                    .build())
                            .build());
//                    Result<com.shenhaofeng.demo.Result<User>> result =
//                            call.execute(new Types<com.shenhaofeng.demo.Result<User>>() {
//                            });

                    call.enqueue(new Types<com.shenhaofeng.demo.Result<User>>() {
                    }, new ResultCallback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            System.out.println();
                        }


                        @Override
                        public void onResult(Call call, Result result) throws IOException {
                            System.out.println();
                        }
                    });


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
